package org.jypj.dev.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jypj.dev.model.CodeData;
import org.jypj.dev.model.NoticeFile;
import org.jypj.dev.model.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.INoticeFileService;
import org.jypj.dev.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * NoticeFile控制器
 * 
 * @author ChenYu
 *
 */
@Controller
@RequestMapping("noticeFileController")
public class NoticeFileController {

	@Resource
	private ICodeDataService codeDataService;

	@Resource
	private INoticeFileService noticeFileService;

	@RequestMapping(value = "toNoticeFileList.action")
	public String toNoticeFileList() {

		return "back/bus/notice_main";
	}

	/**
	 * 查询所有数据接口
	 * 
	 * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
	 */
	@RequestMapping("selectAllNoticeFile.action")
	@ResponseBody
	public Map<String, Object> selectAllNoticeFile() {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			List<NoticeFile> noticeFiles = noticeFileService.selectAllByMap(new HashMap<String, Object>());
			map.put("datas", noticeFiles);
		} catch (Exception e) {
			code = -1;
		}
		map.put("code", code);
		return map;
	}

	/**
	 * 查询所有分类
	 * @return
	 */
	@RequestMapping("selectAllCategories.action")
	@ResponseBody
	public Map<String,Object> selectAllCategories(){
		Map<String,Object> dataMap =new HashMap<>();
		Map<String,Object> queryMap =new HashMap<>();
		int code =0;
		queryMap.put("code","fjfl");
		try {
			List<CodeData> codeDatas = codeDataService.selectAllByMap(queryMap);
			dataMap.put("datas",codeDatas);
		} catch (Exception e) {
			code=-1;
			e.printStackTrace();
		}
		dataMap.put("code",code);
		return dataMap;
	}
	/**
	 *
	 * @param file
	 * @param request
	 * @return code =-1 表示上传失败 -2 表示session超时 -3 表示文件不存在
	 */
	@RequestMapping("uploadFile.action")
	@ResponseBody
	public Map<String, Object> uploadFile(MultipartFile file, HttpServletRequest request,String category) {
		Map<String, Object> map = new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int code = 0;
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			code = -2;
			map.put("code", code);
			return map;
		}
		if (file == null) {
			code = -3;
		} else {
			NoticeFile nf = null;
			try {
				String filePath = FileUtil.upload("/notice", file);
				String fileName = file.getOriginalFilename();
				nf = new NoticeFile();
				nf.setUploaderName(user.getUsername());
				nf.setFileName(fileName);
				nf.setUploadTime(sdf.format(new Date()));
				nf.setFileType(fileName.substring(fileName.lastIndexOf(".") + 1));
				nf.setFilePath(filePath);
				nf.setCategory(category);
				map.put("data", nf);
			} catch (Exception e) {
				e.printStackTrace();
				code = -1;
			}
			if (code == 0) {
				try {
					noticeFileService.saveNoticeFile(nf);
				} catch (Exception e) {
					e.printStackTrace();
					code = -4;
				}
			}

		}
		map.put("code", code);
		return map;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteNoticeFile.action")
	@ResponseBody
	public Map<String, Object> deleteNoticeFile(String id) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			noticeFileService.deleteNoticeFileById(id);
		} catch (Exception e) {
			code = -1;
		}
		map.put("code", code);
		return map;
	}
}